package com.lwt.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lwt.gulimall.product.entity.Attr;
import com.lwt.gulimall.product.entity.AttrAttrgroupRelation;
import com.lwt.gulimall.product.service.AttrAttrgroupRelationService;
import com.lwt.gulimall.product.service.AttrService;
import com.lwt.gulimall.product.service.CategoryService;
import com.lwt.gulimall.product.vo.AttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lwt.gulimall.product.entity.AttrGroup;
import com.lwt.gulimall.product.service.AttrGroupService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwt.gulimall.common.utils.R;



/**
 * 属性分组
 *
 * @author liuweitao
 * @email liuweitao2541@gmail.com
 * @date 2024-01-26 16:11:42
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrService attrService;


    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") String catId){
        long pageSize = Long.parseLong((String)params.get("page"));
        long limit = Long.parseLong((String)params.get("limit"));
        String key = (String)params.get("key");
        System.out.println("====t==="+ params.get("t"));
        Page<AttrGroup> page = new Page<>(pageSize,limit);
        LambdaQueryWrapper<AttrGroup> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(catId)&&!catId.equals("0")){
            wrapper.eq(AttrGroup::getCatelogId, catId);
        }
        if(StringUtils.isNotBlank(key)){
            wrapper.and(w->w.eq(AttrGroup::getAttrGroupId,key)
                    .or()
                    .like(AttrGroup::getAttrGroupName,key));
        }
        Page<AttrGroup> pageResult = attrGroupService.page(page, wrapper);

        return R.ok().put("page", pageResult);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroup attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupVo attrGroupVo = new AttrGroupVo();
        BeanUtils.copyProperties(attrGroup,attrGroupVo);
        Long[] categoryPath = categoryService.getCategoryPath(attrGroup.getCatelogId());
        attrGroupVo.setCatelogIds(categoryPath);
        return R.ok().put("attrGroup", attrGroupVo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrGroup attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrGroup attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @GetMapping("{attrgroupId}/attr/relation")
    public R delete(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrAttrgroupRelation> list = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelation>().eq(AttrAttrgroupRelation::getAttrGroupId, attrgroupId));
        List<Attr> attrs = attrService.listByIds(list.stream().map(AttrAttrgroupRelation::getAttrId).collect(Collectors.toList()));

        return R.ok().put("data",attrs);
    }

}
